package com.fancy.learncenter.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.CartoonUnCoorectBean;
import com.fancy.learncenter.common.JumpIntentKey;
import com.fancy.learncenter.common.MyApplication;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.Utils;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Response;

public class CartoonErrorCenterActivity extends BaseActivity implements View.OnClickListener {


    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.not_correction_question_num)
    TextView notCorrectionQuestionNum;
    @Bind(R.id.not_correction_layout)
    RelativeLayout notCorrectionLayout;
    @Bind(R.id.has_corrected)
    ImageView hasCorrected;
    @Bind(R.id.error_kinds)
    ImageView errorKinds;
    private String unCorrectNums;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCartoonContentView(R.layout.activity_cartoon_error_center, Status.STATUS);
        ButterKnife.bind(this);
        initClick();

    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initClick() {

        notCorrectionLayout.setOnClickListener(this);
        hasCorrected.setOnClickListener(this);
        errorKinds.setOnClickListener(this);
    }

    /**
     * 获取未订正的数量
     */
    private void initData() {

        Map<String, Object> map = new HashMap<>();
        map.put("classId", MyApplication.classIDStr);
        HttpMehtod.getInstance().cartoonCountUnCorrect(map, new CustomNetSubscriber(this) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {

                BaseBean<CartoonUnCoorectBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), CartoonUnCoorectBean.class);

                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<CartoonUnCoorectBean>() {
                    @Override
                    public void success(CartoonUnCoorectBean dataBean) {

                        if (dataBean != null) {

                            unCorrectNums = dataBean.getUnCorrectNums();
                            notCorrectionQuestionNum.setText(unCorrectNums);

                        }
                    }

                    @Override
                    public void fail(String errorCode) {

                    }
                }).dealData();

            }
        });
    }

    private void jumpToActivity(JumpIntentKey.QUESTION_ENTER_TYPE type){

        Intent intent = new Intent(getBaseContext(), CartoonErrorUnitListActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(JumpIntentKey.QUESTION_ENTER,type);
        bundle.putString(JumpIntentKey.NOT_CORRECTED_NUM,unCorrectNums);
        intent.putExtras(bundle);
        startActivity(intent);
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.not_correction_layout:
                jumpToActivity(JumpIntentKey.QUESTION_ENTER_TYPE.NOT_CORRECTED);
                break;
            case R.id.has_corrected:
                jumpToActivity(JumpIntentKey.QUESTION_ENTER_TYPE.HAS_CORRECTED);
                break;
            case R.id.error_kinds:
                jumpToActivity(JumpIntentKey.QUESTION_ENTER_TYPE.ERROR_KINDS);
                break;


        }
    }
}
